#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/stat.h>


struct data
{
	int id;
	char name[128];
};


int main(int argc, char **argv)
{
	if(argc<=1)
	{
		printf("params error...\n");
		return 1;
	}

	int fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 00777);
	if(fd<0)
	{
		printf("open file error...");
		return 1;
	}

	lseek(fd, sizeof(struct data)*100, SEEK_SET);

	if(write(fd, "a", 1)==-1)
	{
		printf("write error!");
		return 1;
	}

	struct data * p_data = (struct data*)mmap(NULL, 
											sizeof(struct data)*100, 
											PROT_READ|PROT_WRITE, 
											MAP_SHARED, 
											fd, 
											0);
	struct data * p_bak = p_data;
	close(fd);

	struct data d;
	d.id = 12028;
	strcpy(d.name, "abc,HI");

	int i;
	for(i=0;i<100;i++)
	{
		memcpy((void*)p_data, (void*)&d, sizeof(d));
		p_data++;
		d.id++;
	}

	printf("initialized over\n");
	getchar();
	munmap(p_bak, sizeof(struct data)*100);

	return 0;
}
